<?php

namespace App\Modules\Api\Http\Controllers;
use App\Jobs\AsyncSendMsgNotice;
use App\Models\Ceshi;
use App\Models\FontAwesome;
use App\Models\Order;
use App\SearchBuilders\OrderSearchBuilder;
use App\Services\Api\EsService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Services\Api\ApiService;
use App\Services\Api\PushService;
use Illuminate\Support\Facades\Log;
use Elasticsearch\ClientBuilder;
// 制定允许其他域名访问
class BaseController extends Controller
{

    public function ceshi(Request $request){
        return 122222222;
        $page = $request->input('page',1);
        $per_page = $request->input('per_page',10);
        $keywords = $request->input('keywords');
        $order_status = $request->input('order_status',0);
        $filters = $request->input('filters');//规格
        $searchBuilder = new OrderSearchBuilder();
        $builder = $searchBuilder->paginate($page,$per_page)->orderBy('order_amount','desc');
        if($keywords){
            $keywords = array_filter(explode(' ', $keywords));
            $builder->keywords($keywords);
        }
        if($filters){
            // 将获取到的字符串用符号 | 拆分成数组
            $filterArray = explode('|', $filters);
            foreach ($filterArray as $filter) {
                // 将字符串用符号 : 拆分成两部分并且分别赋值给 $name 和 $value 两个变量
                list($name, $value) = explode(':', $filter);
                $builder->propertyFilter($name,$value,'filter');
            }
        }
        $result = app('es')->search($builder->getParams());
//        $service = new EsService();
//        $result = $service -> search($page,$per_page,'order_amount','desc',$order_status,$keywords,$filter);
//        $orderIds = collect($result['hits']['hits'])->pluck('_id')->all();
//        $orders = Order::query()->whereIn('id',$orderIds)->orderByRaw(sprintf("FIND_IN_SET(id, '%s')", join(',', $orderIds)))->get();
        $properties = [];
        if (isset($result['aggregations'])) {
            // 使用 collect 函数将返回值转为集合
            $properties = collect($result['aggregations']['properties']['properties']['buckets'])
                ->map(function ($bucket) {
                    // 通过 map 方法取出我们需要的字段
                    return [
                        'key'    => $bucket['key'],
                        'values' => collect($bucket['value']['buckets'])->pluck('key')->all(),
                    ];
                });
        }
        dd($properties);
        dd($result);
//        return json_encode(['data'=>$orders->toArray(),'total'=>$result['hits']['total']['value']]);
    }

    /**
     * 发送验证码接口
     * @param Request $request
     * @method sendCode
     * @author chengzhigang<1256699215@qq.com>
     */
    public function sendCode(Request $request){
        $phone = $request -> input('phone');
        if(empty($phone)||!checkPhone($phone)){
            return apiResponse('请输入正确手机号码');
        }
        $ApiService = new ApiService();
        $result = $ApiService->sendCode($phone);
        return apiResponse($result['msg'],$result['status'],$result['data']);
    }

    /**
     * 上传单文件
     * @param Request $request
     * @method upload
     * @author chengzhigang<1256699215@qq.com>
     */
    public function upload(Request $request){
        $file = $request -> file('file');
        $ext = $request -> input('ext','image');
        $result = upload($file,$ext);
        return apiResponse($result['msg'],$result['status'],['file'=>$result['data']]);
    }

    /**
     * uni-app获取省市区数据
     * @param Request $request
     * @method
     * @author chengzhigang<1256699215@qq.com>
     * @param
     * @return
     */
    public function getUniRegion(Request $request){
        $ApiService = new ApiService();
        $result = $ApiService->getUniRegion();
        return json_encode($result['data']);
    }

    public function pushMsg(Request $request){
        $title = $request -> input('title','测试');
        $content = $request -> input('content','测试队列内容');
        $delay = $request -> input('delay',1);
        $user_type = $request -> input('user_type',1);
        $logo = 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=2668268226,1765897385&fm=26&gp=0.jpg';
        $msgData = ['user_type'=>$user_type,'title'=>$title,'content'=>$content,'logo'=>$logo,'payload'=>['id'=>1,'title'=>$title,'content'=>$content,'logo'=>$logo]];
        dispatch(new AsyncSendMsgNotice($msgData,[1], $delay));
    }



}
